<?php

declare(strict_types=1);

namespace app\operations\validate\admin;

use think\Validate;
use app\operations\model\Category as CategoryModel;

class Category extends Validate
{
    protected $rule = [
        'organ_id' => 'require|checkDuplicate',
        'type' => 'require',
        'category_id' => 'require',
    ];

    protected $message = [
        'organ_id.require' => '上传规则错误',
        'type.require' => '店铺类型不正确',
        'category_id.require' => '请填写品牌名称',
        'organ_id.checkDuplicate' => '已有数据，不可重复提交',
    ];

    protected $scene = [
        'add' => ['organ_id', 'type', 'category_id'],
        'edit' => ['category_id'],
    ];

    // 自定义验证方法
    protected function checkDuplicate($value, $rule, $data = [])
    {
        // 检查是否存在相同的 category_id 和 organ_id
        $count = CategoryModel::where('organ_id', $value)
            ->where('category_id', $data['category_id'])
            ->count();

        return $count > 0 ? false : true;
    }

}
